package com.ershu.user.dao;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.ershu.role.domain.Role;
import com.ershu.user.domain.User;

import cn.itcast.jdbc.TxQueryRunner;

public class UserDao {
	private QueryRunner qr = new TxQueryRunner();
	/**
	 * 
	 * @param id
	 * @return user
	 * @author HaiShin
	 * 通过User的Id封装User的信息并返回
	 */
	public User findById(String id){
		User user = new User();
		Role role = new Role();
		try{
			String sql = "select * from user where id = ?";
			user = qr.query(sql, new BeanHandler<User>(User.class),id);
			sql = "select role_id from user where id = ?";
			role.setId((String)qr.query(sql, new ScalarHandler(),id));
			user.setRole(role); 
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}
		return user;
	}
}
